package pl.asoft.nbp;

import org.joda.time.DateTime;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pl.asoft.nbp.dao.CurrencyDAO;
import pl.asoft.nbp.model.Currency;
import pl.asoft.nbp.model.ExchangeRate;
import pl.asoft.nbp.service.ExchangeRateService;
import pl.asoft.nbp.service.NbpService;
import pl.asoft.nbp.service.impl.NbpServiceImpl;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;

/**
 * Created by rbiedrawa on 2014-12-08.
 */
public class NbpSpringService {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");
        ExchangeRateService exchangeRateService = (ExchangeRateService) context.getBean("exchangeRateService");

//
//        NbpService nbpService = new NbpServiceImpl();
//        Collection<ExchangeRate> exchangeRates = null;
////        "2014-12-04"
//        exchangeRates = nbpService.getExchangeRate("ALL",DateTime.parse("2005-01-01").toDate() ,DateTime.parse("2014-12-04").toDate());
//
//        exchangeRateService.addExchangeRate((java.util.List<ExchangeRate>) exchangeRates);


        System.err.println("--------------------findAllExistingExchangeRates-----------------------------");
        System.err.println(exchangeRateService.findAllExistingExchangeRates().size());
        System.err.println(exchangeRateService.findAllExistingExchangeRates("EUR").size());

        System.err.println("--------------------getAllAboveAverage-----------------------------");
        System.err.println("All: " + exchangeRateService.findAllExistingExchangeRates("USD").size());
        System.err.println("Above: " + exchangeRateService.getAllAboveAverage("USD").size());
        System.err.println("Below: " + exchangeRateService.getAllBelowAverage("USD").size());

        System.err.println("--------------------getExchangeRateForDate-----------------------------");
        System.err.println(exchangeRateService.getExchangeRateForDate(DateTime.parse("2014-12-02").toDate()).size());

        System.err.println("--------------------findAllByDateBetween-----------------------------");
        System.err.println(exchangeRateService.findAllByDateBetween(DateTime.parse("2014-11-02").toDate(),
                                                                    DateTime.parse("2014-12-02").toDate()).size());

        System.err.println(exchangeRateService.findAllByDateBetween("USD", DateTime.parse("2014-11-02").toDate(),
                DateTime.parse("2014-12-02").toDate()));

        System.err.println("--------------------findByCurrencyCodeAndDate-----------------------------");
        System.err.println(exchangeRateService.findByCurrencyCodeAndDate("USD", DateTime.parse("2014-12-02").toDate()));


        System.err.println("--------------------getAverageForCurrencyCode-----------------------------");
        System.err.println(exchangeRateService.getAverageForCurrencyCode("USD"));
        System.err.println(exchangeRateService.getAverageForCurrencyCode("USD",DateTime.parse("2014-11-02").toDate(),
                DateTime.parse("2014-12-03").toDate()));


        System.err.println("--------------------withMaxPriceForCurrencyCode-----------------------------");
        System.err.println(exchangeRateService.withMaxPriceForCurrencyCode("USD"));
        System.err.println(exchangeRateService.withMaxPriceForCurrencyCode("USD", DateTime.parse("2014-11-02").toDate(),
                DateTime.parse("2014-12-03").toDate()));

        System.err.println("--------------------withMinPriceForCurrencyCode-----------------------------");
        System.err.println(exchangeRateService.withMinPriceForCurrencyCode("USD"));
        System.err.println(exchangeRateService.withMinPriceForCurrencyCode("EUR", DateTime.parse("2014-11-02").toDate(),
                DateTime.parse("2014-12-03").toDate()));


        System.err.println("--------------------Best Limit by 100-----------------------------");
        System.err.println(exchangeRateService.getBestExchangeRates("USD", 100).size());
        System.err.println("--------------------Worst Limit by 5-----------------------------");
        System.err.println(exchangeRateService.getWorstExchangeRates("USD", 5).size());


//        System.err.println("--------------------CRUD-----------------------------");
//        ExchangeRate exchangeRate = new ExchangeRate();
//        Currency currency = new Currency();
//        currency.setShortName("USD");
//        exchangeRate.setCurrency(currency);
//        exchangeRate.setDateOfPublication(new Date());
//        System.err.println(exchangeRateService.addExchangeRate(exchangeRate));
//
//        exchangeRate.setPrice(BigDecimal.TEN);
//        exchangeRate.getCurrency().setName("dolar amerykanski");
//        exchangeRate.getCurrency().setConversion(1.0);
//        System.err.println(exchangeRateService.updateExchangeRate(exchangeRate));
//        exchangeRateService.removeExchangeRate(exchangeRate);
//
//        System.err.println(exchangeRateService.findAllExistingExchangeRates().size());
//
//        CurrencyDAO currencyDAO = (CurrencyDAO) context.getBean("currencyDAO");
//
//        Currency currency1 = currencyDAO.find("USD");
//        currency1.setShortName("USD3");
//        currencyDAO.saveOrUpdate(currency1);

    }
}
